package lab3;

import static org.junit.Assert.*;
import org.junit.Test;

/**
 * 版本2.0 表达式继承结构测试类 - 使用JUnit框架
 * 测试抽象算式类和其子类的功能
 */
public class ExpressionInheritanceTest {

    /**
     * 测试加法表达式
     */
    @Test
    public void testAdditionExpression() {
        AbstractArithmeticExpression addExpr = new AdditionExpression(5.0, 3.0);
        assertEquals(8.0, addExpr.calculate(), 0.001);
        assertEquals(5.0, addExpr.getOperand1(), 0.001);
        assertEquals(3.0, addExpr.getOperand2(), 0.001);
        assertEquals('+', addExpr.getOperatorSymbol());
        
        // 测试toString方法
        String expected = "5.00 + 3.00 = 8.00";
        assertEquals(expected, addExpr.toString());
    }

    /**
     * 测试减法表达式
     */
    @Test
    public void testSubtractionExpression() {
        AbstractArithmeticExpression subExpr = new SubtractionExpression(10.0, 4.0);
        assertEquals(6.0, subExpr.calculate(), 0.001);
        assertEquals(10.0, subExpr.getOperand1(), 0.001);
        assertEquals(4.0, subExpr.getOperand2(), 0.001);
        assertEquals('-', subExpr.getOperatorSymbol());
        
        // 测试toString方法
        String expected = "10.00 - 4.00 = 6.00";
        assertEquals(expected, subExpr.toString());
    }

    /**
     * 测试多态性
     */
    @Test
    public void testPolymorphism() {
        // 创建两个不同类型的表达式，但使用父类引用
        AbstractArithmeticExpression addExpr = new AdditionExpression(2.5, 3.5);
        AbstractArithmeticExpression subExpr = new SubtractionExpression(7.0, 2.0);
        
        // 测试多态行为
        assertEquals(6.0, addExpr.calculate(), 0.001);
        assertEquals(5.0, subExpr.calculate(), 0.001);
        assertEquals('+', addExpr.getOperatorSymbol());
        assertEquals('-', subExpr.getOperatorSymbol());
    }

    /**
     * 测试负数运算
     */
    @Test
    public void testNegativeNumbers() {
        AbstractArithmeticExpression addExpr = new AdditionExpression(-5.0, 3.0);
        assertEquals(-2.0, addExpr.calculate(), 0.001);
        
        AbstractArithmeticExpression subExpr = new SubtractionExpression(5.0, -3.0);
        assertEquals(8.0, subExpr.calculate(), 0.001);
    }

    /**
     * 测试小数运算
     */
    @Test
    public void testDecimalNumbers() {
        AbstractArithmeticExpression addExpr = new AdditionExpression(1.11, 2.22);
        assertEquals(3.33, addExpr.calculate(), 0.001);
        
        AbstractArithmeticExpression subExpr = new SubtractionExpression(5.55, 2.22);
        assertEquals(3.33, subExpr.calculate(), 0.001);
    }

    // JUnit测试类不需要main方法，通过JUnit运行器自动执行所有@Test注解的方法
}